﻿
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;
using BIZModel;

/// <summary>
/// Summary description for myHandler
/// </summary>

namespace BIZMA.BL.Handlers
{
    public class FileHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            try
            {   // We need to get the file name that the user is requesting.
                string requestedFile = context.Request.Url.Segments[context.Request.Url.Segments.Length - 1];
                Guid fileId = Guid.Parse(requestedFile);

                if (fileId != Guid.Empty)
                {

                    BIZMAEntities ent = BaseBL.GetEnt();
                    File file = ent.Files.FirstOrDefault(q => q.FileId == fileId);

                    context.Response.ContentType = file.ContentType;
                    context.Response.AddHeader("Content-Disposition", "filename=" + file.FileName);
                    context.Response.BinaryWrite(file.FileData);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString()); throw;
            }
        }


        //private bool HasPermission(HttpContext context, string Filename)
        //{
        //    try
        //    {
        //        if (!context.User.Identity.IsAuthenticated)
        //            return false;

        //        // Determine if the user has permissions here
        //        if (true == true)
        //        {
        //            return true;
        //        }
        //        else
        //        {
        //            return false;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        return false;
        //    }
        //}

      
        
    }
}
